package monster.com.cvh.activity;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.adapter.MyCommentAdater;
import monster.com.cvh.bean.MyCommentBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.RecyclerViewDivider;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.StatusBarUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/4/2
 * @function: 消息通知：我的提醒
 */
public class MyCommentActivity extends PermissionActivity implements View.OnClickListener {
    @BindView(R.id.iv_news_details_title_back)
    ImageView ivNewsDetailsTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_my_comment_guide)
    ImageView ivMyCommentGuide;
    @BindView(R.id.details_title)
    RelativeLayout detailsTitle;
    @BindView(R.id.recyclerview_my_comment)
    RecyclerView recyclerviewMyComment;
    @BindView(R.id.ll_comment_cut)
    LinearLayout llCommentCut;
    @BindView(R.id.rf_activity_looked)
    SmartRefreshLayout rfActivityMyComment;
    private EasyPopup mCirclePop;
    private TextView mTvCircleCommentAll;
    private TextView mTvCircleCommentGet;
    private TextView mTvCircleCommentSend;
    private List<MyCommentBean.DataBean> mDatas = new ArrayList<>();
    private int page = 0;
    private boolean isFirstIn = true;
    private MyCommentAdater myCommentAdater;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_my_comment;
    }

    @Override
    protected void initEvent() {
        llCommentCut.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(MyCommentActivity.this, Color.WHITE);
        mCirclePop = new EasyPopup(MyCommentActivity.this);
        if (isFirstIn) {
            rfActivityMyComment.autoRefresh();
        }
        recyclerviewMyComment.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewMyComment.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, Color.parseColor("#888888")));
        myCommentAdater = new MyCommentAdater(R.layout.item_comment, mDatas);
        rfActivityMyComment.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshAlllayout(refreshlayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshAlllayout(refreshlayout, true);
            }
        });

    }

    /**
     * 后退按钮
     */
    @OnClick(R.id.iv_news_details_title_back)
    public void onIvNewsDetailsTitleBackClicked() {
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_comment_cut:
                mCirclePop.setContentView(R.layout.layout_circle_comment)
                        .setAnimationStyle(R.style.CirclePopAnim)
                        //是否允许点击PopupWindow之外的地方消失
                        .setFocusAndOutsideEnable(true)
                        .createPopup();
                mCirclePop.showAtAnchorView(view, VerticalGravity.BELOW, HorizontalGravity.CENTER, 0, 0);
                mTvCircleCommentAll = mCirclePop.getView(R.id.tv_circle_comment_all);
                mTvCircleCommentGet = mCirclePop.getView(R.id.tv_circle_comment_get);
                mTvCircleCommentSend = mCirclePop.getView(R.id.tv_circle_comment_send);
                ivMyCommentGuide.setImageResource(R.mipmap.mine_comment_down);
                mTvCircleCommentAll.setOnClickListener(this);
                mTvCircleCommentGet.setOnClickListener(this);
                mTvCircleCommentSend.setOnClickListener(this);
                switch (tvTitle.getText().toString()){
                    case "所有评论":
                        setBackground(0);
                        break;
                    case "我收到的":
                        setBackground(1);
                        break;
                    case "我发出的":
                        setBackground(2);
                        break;
                }

                break;
            case R.id.tv_circle_comment_all:
                tvTitle.setText("所有评论");
                rfActivityMyComment.autoRefresh();
                rfActivityMyComment.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
                    @Override
                    public void onLoadmore(RefreshLayout refreshlayout) {
                        refreshAlllayout(refreshlayout, false);
                    }

                    @Override
                    public void onRefresh(RefreshLayout refreshlayout) {
                        refreshAlllayout(refreshlayout, true);
                    }
                });
                mCirclePop.dismiss();
                setBackground(0);
                ivMyCommentGuide.setImageResource(R.mipmap.mine_comment_up);

                break;
            case R.id.tv_circle_comment_get:
                setBackground(1);
                tvTitle.setText("我收到的");
                rfActivityMyComment.autoRefresh();
                rfActivityMyComment.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
                    @Override
                    public void onLoadmore(RefreshLayout refreshlayout) {
                        receiverRefreshAlllayout(refreshlayout, false);
                    }

                    @Override
                    public void onRefresh(RefreshLayout refreshlayout) {
                        receiverRefreshAlllayout(refreshlayout, true);
                    }
                });
                mCirclePop.dismiss();
                ivMyCommentGuide.setImageResource(R.mipmap.mine_comment_up);
                break;
            case R.id.tv_circle_comment_send:
                tvTitle.setText("我发出的");
                rfActivityMyComment.autoRefresh();
                rfActivityMyComment.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
                    @Override
                    public void onLoadmore(RefreshLayout refreshlayout) {
                        sendRefreshAlllayout(refreshlayout, false);
                    }

                    @Override
                    public void onRefresh(RefreshLayout refreshlayout) {
                        sendRefreshAlllayout(refreshlayout, true);
                    }
                });
                mCirclePop.dismiss();
                setBackground(2);
                ivMyCommentGuide.setImageResource(R.mipmap.mine_comment_up);
                break;
        }
    }

    /**
     * 获取所有评论
     * @param refreshlayout 刷新
     * @param b
     */
    private void refreshAlllayout(final RefreshLayout refreshlayout, final boolean b) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (b) {
                    if (mDatas.size() > 0) {
                        mDatas.clear();
                        page = 0;
                    }
                    getComment(MyConstant.GET_ALL_COMMENT,page);
                    recyclerviewMyComment.setAdapter(myCommentAdater);
                    rfActivityMyComment.finishRefresh();
                    page = 0;

                } else {
                    getComment(MyConstant.GET_ALL_COMMENT,page);
                    rfActivityMyComment.finishLoadmore();
                    page++;
                }
            }
        }, 2000);
    }
    /**
     * 获取发出的评论
     * @param refreshlayout 刷新
     * @param b
     */
    private void sendRefreshAlllayout(final RefreshLayout refreshlayout, final boolean b) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (b) {
                    if (mDatas.size() > 0) {
                        mDatas.clear();
                        page = 0;
                    }
                    getComment(MyConstant.GET_SEND_COMMENT,page);
                    recyclerviewMyComment.setAdapter(myCommentAdater);
                    rfActivityMyComment.finishRefresh();
                    page = 0;

                } else {
                    getComment(MyConstant.GET_SEND_COMMENT,page);
                    rfActivityMyComment.finishLoadmore();
                    page++;
                }
            }
        }, 2000);
    }

    /**
     * 获取收到评论
     * @param refreshlayout 刷新
     * @param b
     */
    private void receiverRefreshAlllayout(final RefreshLayout refreshlayout, final boolean b) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (b) {
                    if (mDatas.size() > 0) {
                        mDatas.clear();
                        page = 0;
                    }
                    getComment(MyConstant.GET_RECEIVE_COMENT,page);
                    recyclerviewMyComment.setAdapter(myCommentAdater);
                    rfActivityMyComment.finishRefresh();
                    page = 0;

                } else {
                    getComment(MyConstant.GET_RECEIVE_COMENT,page);
                    rfActivityMyComment.finishLoadmore();
                    page++;
                }
            }
        }, 2000);
    }

    /* 获取评论
     * @param page 页数
     */
    private void getComment(String url ,int page) {
        OkGo.<String>get(url).params("token", SPUtils.getString(MyCommentActivity.this,"token",""))
                .params("page_index",page).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Gson gson = new Gson();
                Log.i("lixiaofei", "onSuccess: "+s);
                MyCommentBean myCommentBean = gson.fromJson(s, MyCommentBean.class);
                switch (myCommentBean.getCode()){
                    case 1:
                        List<MyCommentBean.DataBean> data = myCommentBean.getData();
                        if (data !=null && data.size()>0 ){
                            myCommentAdater.addData(data);
                        }
                        break;
                    case -1002:
                        break;
                    case -1003:
                        break;
                    case -1004:
                        break;
                }
            }
        });
    }

    private void setBackground(int i) {
        switch (i) {
            case 0:
                mTvCircleCommentAll.setBackgroundResource(R.drawable.bg_cut_comment);
                mTvCircleCommentAll.setTextColor(Color.WHITE);
                mTvCircleCommentGet.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentGet.setTextColor(Color.parseColor("#3EC8D1"));
                mTvCircleCommentSend.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentSend.setTextColor(Color.parseColor("#3EC8D1"));
                break;
            case 1:
                mTvCircleCommentGet.setBackgroundResource(R.drawable.bg_cut_comment);
                mTvCircleCommentGet.setTextColor(Color.WHITE);
                mTvCircleCommentAll.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentAll.setTextColor(Color.parseColor("#3EC8D1"));
                mTvCircleCommentSend.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentSend.setTextColor(Color.parseColor("#3EC8D1"));
                break;
            case 2:
                mTvCircleCommentSend.setBackgroundResource(R.drawable.bg_cut_comment);
                mTvCircleCommentSend.setTextColor(Color.WHITE);
                mTvCircleCommentAll.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentAll.setTextColor(Color.parseColor("#3EC8D1"));
                mTvCircleCommentGet.setBackgroundResource(R.drawable.bg_cut_comment_nomal);
                mTvCircleCommentGet.setTextColor(Color.parseColor("#3EC8D1"));
                break;
        }
    }

}
